package com.ibeeking.found.common.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * url处理
 *
 * @Description
 * @Author ibeeking
 * @Date 2021-01-06
 **/
public class UrlUtils {
    private static final String LINE = "/";
    private static final String DOUBLE_LINE = "//";
    private static final String HTTP = "http";

    /**
     * 去除爱琴海url域名
     *
     * @param url
     * @return
     */
    public static String removeMallUrlDomainName(String mallUrl, String url) {
        if (StringUtils.isAnyBlank(mallUrl, url)) {
            return url;
        }
        url = url.replaceAll(mallUrl, "");
        return removeStartLine(url);
    }

    /**
     * 去掉url中的域名 例如把 "https://www.baidu.com/123" 或 "http://www.baidu.com/123" 转为 '/123'
     *
     * @param url
     * @return
     */
    public static String removeUrlDomainName(String url) {
        if (StringUtils.isBlank(url)) {
            return url;
        }
        if (!url.contains(DOUBLE_LINE)) {
            return removeStartLine(url);
        }
        url = url.replaceFirst(DOUBLE_LINE, "");
        url = url.substring(url.indexOf(LINE) + 1);
        return url;
    }
    /**
     * 符Url转换为URL对象
     *
     * @param urlStr
     * @return
     */
    public static URL parse(String urlStr) {
        URL url;
        try {
            url = new URL(urlStr);
        } catch (MalformedURLException e) {
            return null;
        }
        if (url.getProtocol().equals("") || url.getHost().equals("")) {
            return null;
        }
        return url;
    }

    public static URL checkUrl(String urlStr) {
        URL tinyURL = UrlUtils.parse(urlStr);
        if (tinyURL == null) {
            throw null;
        }
        return tinyURL;
    }
    /**
     * 把域名和路径合并 例如把 "https://www.baidu.com/" 和 "/123" 合并为 "https://www.baidu.com/123"
     *
     * @param domainName 例如 "https://www.baidu.com/"
     * @param path       例如 "/123/aaa"
     * @return
     */
    public static String addUrlDomainName(String domainName, String path) {
        if (StringUtils.isAnyBlank(domainName, path)) {
            return null;
        }
        if (path.startsWith(HTTP)) {
            return path;
        }
        return (domainName.endsWith(LINE) ? domainName : domainName + LINE) + (path.startsWith(LINE) ? path.substring(1) : path);
    }

    /**
     * 去掉字符串头部的/
     */
    private static String removeStartLine(String str) {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        return str.startsWith(LINE) ? str.substring(1) : str;
    }


    /**
     * 从请求中获取域名
     * 例如 http://sit-found-open-api.ibeeking.com/openApi/v1/open/api/event 返回  http://sit-found-open-api.ibeeking.com
     *
     * @param request
     * @return
     */
    public static String getHost(HttpServletRequest request) {
        if (request == null) {
            return com.ibeeking.nematos.utils.data.StringUtils.EMPTY;
        }
        StringBuffer url = request.getRequestURL();
        return url.delete(url.length() - request.getRequestURI().length(), url.length()).toString();
    }

    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis());
    }
}
